package com.itheima.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Slf4j
public class JwtUtils {

    private static String signKey = "SVRIRUlNQQ==";
    private static Long expire = 1000*60*60*2L;

    /**
     * 生成JWT令牌
     * @return
     */
    public static String generateJwt(Map<String,Object> claims){
        String jwt = Jwts.builder()
                .addClaims(claims)
                .signWith(SignatureAlgorithm.HS256, signKey)
                .setExpiration(new Date(System.currentTimeMillis() + expire))
                .compact();
        return jwt;
    }

    /**
     * 解析JWT令牌
     * @param jwt JWT令牌
     * @return JWT第二部分负载 payload 中存储的内容
     */
    public static Claims parseJWT(String jwt){
        Claims claims = Jwts.parser()
                .setSigningKey(signKey)
                .parseClaimsJws(jwt)
                .getBody();
        return claims;
    }

   /* public static void main(String[] args) {
        Map<String, Object> map = new HashMap<>();
        map.put("kunkun","jinitiamei1");
        String jwt = generateJwt(map);
        log.info("jwt:{}",jwt);
        Claims claims = parseJWT("eyJhbGciOiJIUzI1NiJ9.eyJrdW5rdW4iOiJqaW5pdGlhbWVpMSIsImV4cCI6MTcxODEyMjQ2MX0.TKukiE-B5RRUnF4QF03XOAaJ2vAlxd8wDnGGcTtA3Hc");
        String kunkun = claims.get("kunkun", String.class);
        log.info("jwt:{}",kunkun);
    }*/
}
